package org.hb0712.discovery.service.impl;

import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.hb0712.discovery.mapper.ImageMapper;
import org.hb0712.discovery.pojo.Image;
import org.hb0712.discovery.service.ImageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ImageServiceImpl extends FileServiceImpl implements ImageService{
	private Logger logger = LoggerFactory.getLogger(ImageServiceImpl.class);
	
	@Autowired
	public ImageMapper imageMapper;
	
	public Set<String> listTime() {
		List<Date> list = imageMapper.listTime();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Set<String> dates = new TreeSet<String>(new Comparator<String>() {
			public int compare(String o1, String o2) {
				int num=o2.compareTo(o1);
				return num;
			}
		});
		for (Date d:list) {
			String sd = sdf.format(d);
			dates.add(sd);
		}
		return dates;
	}
	
	public Set<String> listYear(Set<String> dates) {
		Set<String> years = new TreeSet<String>(new Comparator<String>() {
			public int compare(String o1, String o2) {
				int num=o2.compareTo(o1);
				return num;
			}
		});
		for (String date:dates) {
			String y = date.substring(0, 4);
			years.add(y);
		}
		return years;
	}
	
	public List<Image> list(String date) {
		String start = date+" 00:00:00";
		String end = date+" 23:59:59";
		if (date.length()==7) {
			start = date+"-01 00:00:00";
			end = date+"-31 23:59:59";
		}
		return imageMapper.getImagesByTimes(start, end);
	}
	
}
